#ifndef _GAME_H_
#define _GAME_H_

#include "v-graphs.h"
#include "v-text.h"
#include "sprite.h"
#include "utypes.h"
#include "rtc.h"
#include <unistd.h>

/** @defgroup Game Game
 * @{
 *
 * Game related functions
 */

/** @name Some Game Colors */
/*@{*/ 
#define START_COLOR 2         ///<  start menu color
#define HIGHT_SCORES_COLOR 5  ///<  high scores menu color
#define EXIT_COLOR 4          ///<  exit color
/*@}*/ 

extern int lives; ///<  extern variable lives of player at game


/** Initial credits
 */
__attribute__((unused)) static char * credits[] = { 
    "                                   QIX                                         ",
    "                                                                               ",
    "Qix is a classic Arcade game made in the 80's.                                 ",
    "The objective of Qix is to claim, a supermajority of the playfield.            ",
    "At the start, the playing field is a large, unfilled rectangle, containing some",
    "entities (Linux Penguins) that move within the confines of the rectangle.      ",
    "The player controls a small ball marqer that can move around the edges         ",
    "of the rectangle, with the goal to claim as much screen as possible via drawing",
    "Stix. When the player completes a closed shape, the captured area becomes solid",
    "To complete the game, the player must claim most of the playfield (80%).       ",
    "The player has 3 lives and can lose a live if one of the Penguins touches a    ",
    "Stix line as it's being draw, or the time played reaches 300 seconds.          ",
    "                                                                               ",
    "                                                                               ",
    "                               INSTRUCTIONS                                    ",
    "                                                                               ",
    "Use the Keyboard Arrows to move the player's sprite.                           ",
    "Press the Enter key at the end of each game to go to menu.                     ",
    "In High Scores press right mouse button to go back to menu.                    ",
    "                                                                               ",
    "                                                                               ",
    "Qix made by:                                                                   ",
    "       Andre Rodrigues                                                         ",
    "       Fernando Gracas                                                         ",
    "                                                       LCOM   2009/2010        "};
    
/** Show credits on video-text mode
 */
void show_credits();    

/** Draw backgound: Border, lives, time and penguins
 */
void draw_background(char * base, char* table);

/** Draw Menu: Title, Start, High Scores and Exit buttons
 */
int menu(char* base, char* table);

/** When Player lose a live this function cleans one heart and decrements the number of lives
 */
void lose_life(char *base);

///////////////////PONTUACOES/////////////////////////////

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/** @defgroup Pontucoes Pontucoes
 * @{
 *
 * Pontucoes related functions
 */
 
/** @name Sizes for Pontuacoes */
/*@{*/ 
#define NOME_SIZE 15       ///<  name maximum size
#define PONTUACOES_SIZE 10 ///<  maximum size of pontuation that are showe on te game and that are saved in file
/*@}*/ 

/** Pontuacoes struct
 */
typedef struct {
	int pontos; ///< points
	int dia;    ///< day
	int mes;    ///< month
	int ano;    ///< year
	char* nome; ///< name
} Pontuacao;

/** Initiates a Pontuacao array with "size" elements
 */
void pontuacoesInit(Pontuacao** pontuacoes, int size);

/** Initiates a Pontuacao object
 */
void pontuacaoInit(Pontuacao* pontuacao);

/** Free a Pontuacao array with "size" elements
 */
void pontuacoesFree(Pontuacao** pontuacoes, int size);

/** Free a Pontuacao object
 */
void pontuacaoFree(Pontuacao* pontuacao);

/** Prints in video-graphic mode a Pontuacao array with "size" elements
 */
void printPontuacoes(Pontuacao** pontuacoes, int xt, int xp, int xd, int y, char* base, char* table);

/** Prints in video-graphic mode a Pontuacao object
 */
void printPontuacao(Pontuacao* pontuacao, int xt, int xp, int xd, int y, char* base, char* table);

/** Load Pontuacao from file
 */
int loadPontuacoes(char* filename, Pontuacao** pontuacoes, int size);

/** Compares Pontuacao ex: first the function compares by Pontuacao by points, if they'r equal compares them by date
 * ex1: if two players had the same score, it will prevail the one that reached that score first
 */
int compare_pontuacao (const void * a, const void * b);

/** Save Pontuacao from file
 */
int savePontuacoes(char* filename, Pontuacao** pontuacoes, int size);

/** Show high scores screen, this function calls printPontuacoes
 * @see printPontuacoes
 */
void show_pontuacoes(Pontuacao** arr, int size, char* base, char* table);

/** Returns the minimum score in the first 10 positions, to see if player has entered the top ten
 */
int getPontuacaoMin(Pontuacao** arr, int size);

/** Removes newline from char
 */
void remnewline(char* buffer);

//void organizarPontuacoes(Pontuacao** pontuacoes, Pontuacao* pontuacao, int size);

/** Creates a new Pontuacao pointer
 */
Pontuacao* criaPontuacao(char* nome, int pontos, int dia, int mes, int ano);

/** @} end of Pontucoes */
///////////////////STACK/////////////////////////////

/** @defgroup Stack Stack
 * @{
 *
 * Stack related functions
 */

/** Stack struct
 */
typedef struct {
  int* contents;  ///< array of integer numbers
  int top;        ///< size of stack
  int maxSize;    ///< maxSize of stack
} Stack;

/** Initiates Stack: alocates memory for "size" positions
 */
void initStack(Stack* st, int size);

/** Free Stack
 */
void freeStack(Stack* st);

/** Checks if Stack is empty
 */
Bool emptyStack(Stack* st);

/** Pushes element into Stack
 */
void push(Stack* st, int val);

/** Pops element onto Stack
 */
void pop(Stack* st);

/** Returns the size of Stack
 */
int sizeStack(Stack* st);

/** Returns the top element of Stack
 */
int top(Stack* st);

/** @} end of Pontucoes */

/** @} end of Game */
#endif
